package com.yanqu.road.server.cmd.activity.douluodalu;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.S_DOU_LUO_DA_LU_GET_WIN_REWARD_FROM_CROSS,desc = "领取连胜奖励")
public class DouLuoDaLuGetBattleWinRewardCmdFromCross extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DouLuoDaLuProto.DouLuoDaLuGetBattleWinRewardRespMsg reqMsg = DouLuoDaLuProto.DouLuoDaLuGetBattleWinRewardRespMsg.parseFrom(packet.getBytes());
        ret = reqMsg.getRet();
        if(ret == 0){
            //连胜奖励
            if(reqMsg.hasPatronsExp()){
                PatronsModule patronsModule = player.getModule(PatronsModule.class);
                UserPatrons userPatrons = patronsModule.getUserPatrons(reqMsg.getPatronsId());
                if(userPatrons != null){
                    userPatrons.setBaiYeExp(reqMsg.getPatronsExp() + userPatrons.getBaiYeExp());
                    patronsModule.syncPatronsData(userPatrons);
                }
            }else if(reqMsg.hasRewards()) {
                player.getModule(CurrencyModule.class).addCurrency(reqMsg.getRewards(), eLogMoneyType.DouLuoDaLuActivity, eLogMoneyType.DouLuoDaLuBattleLianShengReward);
            }

            //结算奖励
            if(reqMsg.hasSettle()){
                player.getModule(CurrencyModule.class).addCurrency(reqMsg.getSettle().getRewards(), eLogMoneyType.DouLuoDaLuActivity, eLogMoneyType.DouLuoDaLuBattleSettleReward);

                if(reqMsg.hasLog()) {
                    DouLuoDaLuProto.DouLuoDaLuBattleSettlementTem settle = reqMsg.getSettle();
                    DouLuoDaLuMgr.addPatronsBattleSettleLog(player, reqMsg.getLog(), settle, false);
                }

            }
        }
        player.sendPacket(Protocol.U_DOU_LUO_DA_LU_GET_WIN_REWARD, reqMsg.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
